﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CongThongTin.Entities;

namespace CongThongTin.Administrators
{
    public partial class LoaiTin : System.Web.UI.Page
    {
        CongThongTinEntities _entities=new CongThongTinEntities ();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hienThiLoaiTinTuc();
                loadTheLoaiTinTuc();
            }
        }

        protected void btnThemMoi_Click(object sender, EventArgs e)
        {
            try
            {
                int selectMaTheLoai=Convert.ToInt32(ddlTenTheLoai.SelectedValue);
                Entities.THELOAI oNewTheLoai = _entities.THELOAIs.SingleOrDefault(
                    tl => tl.MATHELOAI == selectMaTheLoai);

                Entities.LOAITIN oNewLoaiTin = new LOAITIN();
                oNewLoaiTin.TENLOAITIN = tbTenLoaiTinTuc.Text.Trim();
                oNewLoaiTin.THELOAI = oNewTheLoai;
                oNewLoaiTin.HIENTHI = true;

                _entities.AddToLOAITINs(oNewLoaiTin);
                _entities.SaveChanges();

                hienThiLoaiTinTuc();

                Response.Redirect(Page.Request.Url.ToString(), true);
            }
            catch (Exception)
            { }
        }

        void hienThiLoaiTinTuc()
        {
            var dulieu= from lt in _entities.LOAITINs
                        join tl in _entities.THELOAIs 
                        on lt.THELOAI.MATHELOAI equals tl.MATHELOAI
                        select new
                        {
                            MALOAITIN=lt.MALOAITIN,
                            TENLOAITIN=lt.TENLOAITIN,
                            TenTheLoaiTinTuc=tl.TENTHELOAI
                        };

            GridView1.DataSource = dulieu.ToList();
            GridView1.DataBind();
        }

        void loadTheLoaiTinTuc()
        {
            ddlTenTheLoai.DataSource = _entities.THELOAIs;
            ddlTenTheLoai.DataValueField = "MATHELOAI";
            ddlTenTheLoai.DataTextField = "TENTHELOAI";
            ddlTenTheLoai.DataBind();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            hienThiLoaiTinTuc();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            
          
            hienThiLoaiTinTuc(); 

        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("lblMaLoaiTin");
            TextBox txtLoaiTin=(TextBox)row.FindControl("txtTenLoaiTin");
           
            int idLoaiTin=int.Parse(lblID.Text);

            LOAITIN oUpdateTheLoai=_entities.LOAITINs.SingleOrDefault(t=>t.MALOAITIN==idLoaiTin);
            oUpdateTheLoai.TENLOAITIN=txtLoaiTin.Text;

            _entities.SaveChanges();

            GridView1.EditIndex = -1;
            hienThiLoaiTinTuc();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            hienThiLoaiTinTuc();
        }
      
    }
}